import Router from 'vue-router';
import Common from '../layout/common.vue'

export const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: Common,
    meta: { title: '首页概览', requiresAuth: true},
    children: [
      {
        path: '/index',
        component: () => import('../pages/index/index.vue'),
        //name: 'common',
        meta: { title: '首页概览', icon: '/favicon.ico' }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../pages/login/login.vue'),
    meta: { title: '登录', icon: '/favicon.ico' }
  },
  {
    path: '/menu',
    name: 'Menu',
    component: Common,
    meta: { title: '菜单列表', requiresAuth: true},
    children: [
      {
        path: '/menu',
        component: () => import('../pages/menu/index.vue'),
        name: 'menu',
        meta: { title: '菜单列表', icon: '/favicon.ico', keepAlive: false }
      }, {
        path: '/menu-create',
        component: () => import('../pages/menu/create.vue'),
        name: 'MenuCreate',
        meta: { title: '菜单添加', icon: '/favicon.ico' }
      }, {
        path: '/menu-update/:id',
        component: () => import('../pages/menu/update.vue'),
        name: 'MenuUpdate',
        meta: { title: '菜单编辑', icon: '/favicon.ico' }
      }
    ]
  },
  {
    path: '/userCenter',
    redirect: { name: 'department' },
    name: 'userCenter',
    component: Common,
    meta: { title: '平台中心', requiresAuth: true},
    children: [
      {
        path: '/department',
        component:()=>import('../pages/department/index.vue'),
        name: 'department',
        meta: { title: '部门管理', icon: '/favicon.ico' }
      }, {
        path: '/department-update/:id',
        component:()=>import('../pages/department/update.vue'),
        name: 'DepartmentUpdate',
        meta: { title: '部门管理详情', icon: '/favicon.ico' }
      }, {
        path: '/role',
        component: () => import('../pages/role/index.vue'),
        name: 'role',
        meta: { title: '角色管理', icon: '/favicon.ico' }
      }, {
        path: '/role-create',
        component: () => import('../pages/role/create.vue'),
        name: 'RoleCreate',
        meta: { title: '角色添加', icon: '/favicon.ico' }
      }, {
        path: '/role-update/:id',
        component: () => import('../pages/role/update.vue'),
        name: 'RoleUpdate',
        meta: { title: '角色管理详情', icon: '/favicon.ico' }
      }, {
        path: '/role-list/:id',
        component: () => import('../pages/role/role.vue'),
        name: 'RoleList',
        meta: { title: '权限管理', icon: '/favicon.ico' }
      }, {
        path: '/user',
        component: () => import('../pages/user/index.vue'),
        name: 'userindex',
        meta: { title: '账号管理', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/user/update/:id',
        component: () => import('../pages/user/update.vue'),
        name: 'UserUpdate',
        meta: { title: '账号管理详情', icon: '/favicon.ico' }
      }, {
        path: '/log',
        component: () => import('../pages/log/index.vue'),
        name: 'log',
        meta: { title: '操作日志', icon: '/favicon.ico' }
      }
    ]
  },
  {
    path: '/orders',
    name: 'orders',
    component: Common,
    meta: { title: '订单中心', requiresAuth: true},
    children: [
      {
        path: '/orders',
        component: () => import('../pages/orders/index.vue'),
        name: 'ordersIndex',
        meta: { title: '订单中心', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/orders/update/:id',
        component: () => import('../pages/orders/update.vue'),
        name: 'ordersUpdate',
        meta: { title: '订单详情', icon: '/favicon.ico' },
      }, {
        path: '/orders/statistics/:id',
        component: () => import('../pages/orders/statistics.vue'),
        name: 'ordersStatistics',
        meta: { title: '订单统计', icon: '/favicon.ico' }
      }, {
        path: '/orders/particulars/:id',
        component: () => import('../pages/orders/particulars.vue'),
        name: 'ordersParticulars',
        meta: { title: '报告详情', icon: '/favicon.ico' }
      },
    ]
  },
  // {
  //   path: '/task',
  //   name: 'task',
  //   component: Common,
  //   meta: { title: '任务中心', requiresAuth: true},
  //   children: [
  //     {
  //       path: '/task',
  //       component: () => import('../pages/task/index.vue'),
  //       name: 'taskIndex',
  //       meta: { title: '任务中心', icon: '/favicon.ico', keepAlive: false },
  //     }, {
  //       path: '/task/update/:id',
  //       component: () => import('../pages/task/update.vue'),
  //       name: 'taskUpdate',
  //       meta: { title: '任务详情', icon: '/favicon.ico' }
  //     }, {
  //       path: '/task/video/:id',
  //       component: () => import('../pages/task/video.vue'),
  //       name: 'testVideo',
  //       meta: { title: '实时视频', icon: '/favicon.ico' }
  //     }
  //   ]
  // },
  {
    path: '/task',
    name: 'task',
    component: Common,
    meta: { title: '任务中心', requiresAuth: true},
    children: [
      {
        path: '/task',
        component: () => import('../pages/task2/index.vue'),
        name: 'taskIndex',
        meta: { title: '任务中心', icon: '/favicon.ico', keepAlive: false },
      }, 
      {
        path: '/task/update/:id',
        component: () => import('../pages/task2/update.vue'),
        name: 'taskUpdate',
        meta: { title: '任务详情', icon: '/favicon.ico' }
      }, 
      // {
      //   path: '/task/video/:id',
      //   component: () => import('../pages/task/video.vue'),
      //   name: 'testVideo',
      //   meta: { title: '实时视频', icon: '/favicon.ico' }
      // }
    ]
  },
  {
    path: '/operateCenter',
    name: 'operateCenter',
    redirect: { name: 'pushIndex' },
    component: Common,
    meta: { title: '运营中心', requiresAuth: true},
    children: [
      {
        path: '/push',
        component: () => import('../pages/push/index.vue'),
        name: 'pushIndex',
        meta: { title: '推送管理', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/push/update/:id',
        component: () => import('../pages/push/update.vue'),
        name: 'pushUpdate',
        meta: { title: '推送管理详情', icon: '/favicon.ico' }
      }, {
        path: '/service',
        component: () => import('../pages/service/index.vue'),
        name: 'serviceindex',
        meta: { title: '服务号管理', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/service/update/:id',
        component: () => import('../pages/service/update.vue'),
        name: 'serviceUpdate',
        meta: { title: '服务号管理', icon: '/favicon.ico' }
      }, {
        path: '/point',
        component: () => import('../pages/point/index.vue'),
        name: 'pointIndex',
        meta: { title: '积分管理', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/point/update/:id',
        component: () => import('../pages/point/update.vue'),
        name: 'pointUpdate',
        meta: { title: '积分管理详情', icon: '/favicon.ico' }
      }, {
        path: '/video',
        component: () => import('../pages/video/index.vue'),
        name: 'videoIndex',
        meta: { title: '实时监控', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/goodscate',
        component: () => import('../pages/goodscate/index.vue'),
        name: 'goodscate',
        meta: { title: '产品分类', icon: '/favicon.ico', keepAlive: false }
      }, {
        path: '/goodscate/create',
        component: () => import('../pages/goodscate/create.vue'),
        name: 'GoodscateCreate',
        meta: { title: '产品分类添加', icon: '/favicon.ico' }
      }, {
        path: '/goodscate/update/:id',
        component: () => import('../pages/goodscate/update.vue'),
        name: 'GoodscateUpdate',
        meta: { title: '产品分类编辑', icon: '/favicon.ico' }
      }, {
        path: '/goods',
        component: () => import('../pages/goods/index.vue'),
        name: 'goods',
        meta: { title: '产品列表', icon: '/favicon.ico', keepAlive: false }
      }, {
        path: '/goods-update/:id',
        component: () => import('../pages/goods/update.vue'),
        name: 'GoodsUpdate',
        meta: { title: '产品编辑', icon: '/favicon.ico' }
      }, {
        path: '/goods/create',
        component: () => import('../pages/goods/create.vue'),
        name: 'GoodsCreate',
        meta: { title: '产品添加', icon: '/favicon.ico' }
      }, {
        path: '/servicemenu',
        component: () => import('../pages/servicemenu/index.vue'),
        name: 'servicemenu',
        meta: { title: '服务号菜单列表', icon: '/favicon.ico', keepAlive: false }
      }, {
        path: '/servicemenu/create',
        component: () => import('../pages/servicemenu/create.vue'),
        name: 'ServiceenuCreate',
        meta: { title: '服务号菜单添加', icon: '/favicon.ico' }
      }, {
        path: '/servicemenu-update/:id',
        component: () => import('../pages/servicemenu/update.vue'),
        name: 'ServiceenuUpdate',
        meta: { title: '服务号菜单编辑', icon: '/favicon.ico' }
      }, {
        path: '/device',
        component: () => import('../pages/device/index.vue'),
        name: 'deviceindex',
        meta: { title: '设备列表', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/device/update/:id',
        component: () => import('../pages/device/update.vue'),
        name: 'deviceUpdate',
        meta: { title: '编辑设备', icon: '/favicon.ico' }
      }, {
        path: '/devicemodel',
        component: () => import('../pages/deviceModel/index.vue'),
        name: 'devicemodelindex',
        meta: { title: '设备型号列表', icon: '/favicon.ico', keepAlive: false },
      }, {
        path: '/devicemodel/update/:id',
        component: () => import('../pages/deviceModel/update.vue'),
        name: 'devicemodelUpdate',
        meta: { title: '编辑设备型号', icon: '/favicon.ico' }
      }
      // ,{
      //   path: '/report',
      //   component: () => import('../pages/report/index.vue'),
      //   name: 'reportIndex',
      //   meta: { title: '报告管理', icon: '/favicon.ico', keepAlive: false },
      // }
      , {
        path: '/report/update/:id',
        component: () => import('../pages/report/update.vue'),
        name: 'reportUpdate',
        meta: { title: '生成报告', icon: '/favicon.ico' }
      },
      {
        path: '/check',
        component: () => import('../pages/check/index.vue'),
        name: 'CheckIndex',
        meta: { title: '巡检管理', icon: '/favicon.ico', keepAlive: false },
      },
      {
        path: '/check/create',
        component: () => import('../pages/check/create.vue'),
        name: 'CheckCreate',
        meta: { title: '巡检管理添加', icon: '/favicon.ico' }
      },
      {
        path: '/check/update/:id',
        component: () => import('../pages/check/update.vue'),
        name: 'CheckUpdate',
        meta: { title: '巡检管理修改', icon: '/favicon.ico' }
      },
      {
        path: '/industry',
        component: () => import('../pages/industry/index.vue'),
        name: 'industry',
        meta: { title: '行业管理', icon: '/favicon.ico', keepAlive: false },
      },
      {
        path: '/industry/create/:sort',
        component: () => import('../pages/industry/create.vue'),
        name: 'industry-create',
        meta: { title: '新建行业管理', icon: '/favicon.ico', keepAlive: false },
      },
      {
        path: '/industry/update/:id',
        component: () => import('../pages/industry/update.vue'),
        name: 'industry-update',
        meta: { title: '行业管理修改', icon: '/favicon.ico', keepAlive: false },
      },
    ]
  },
  {
    path: '/401',
    name: '401',
    component: Common,
    meta: { title: '无访问权限', requiresAuth: true},
    children: [
      {
        path: '/401',
        component: () => import('../pages/401/index.vue'),
        name: '401Index',
        meta: { title: '无访问权限', icon: '/favicon.ico', keepAlive: false },
      }
    ]
  },
]

const router = new Router({
  routes
});
const getUrl = (data,url,has)=>{
  return data.some(item=>{
    console.log(item)
    if(url == item.url){
      return true
    }
    if(item.children.length >0){
        return getUrl(item.children,url,has)
    }
  })
}
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let userInfo = window.localStorage.getItem('user_info')
    //点击新路由定位到顶部
    document.body.scrollTop = 0;
    if (userInfo) {
      let info = JSON.parse(userInfo)
      info.menu.push({url: '/401', children: []})
      let path = to.path
      if (to.params.id !== undefined) {
        path = path.substring(0, path.lastIndexOf('/'))
      }
      if (info.menu.length > 0) {
        // if (getUrl(info.menu, path, false)) {
          next()
        // } else {
        //   if (path == '/index' && getUrl(info.menu, '/orders', false)) {
        //     next({
        //       path: '/orders',
        //     })
        //   } else {
        //     next({
        //       path: '/401',
        //     })
        //   }
        // }
      } else {
        next({
          path: '/401',
        })
      }
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
});

export default router
